﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Scripting.Hosting;
using IronRuby;
using System.Reflection;
using System.IO;
using Flame.Reflection;

namespace Flame.Dlr
{
    public static  class Loader
    {
        private static readonly IronRubyExec x = new IronRubyExec();
    }

    public class IronRubyExec : AExecutablePR
    {

        protected override ScriptEngine ScriptEngine { get { return Ruby.CreateEngine(); } }

        protected override Result checkForSpecialType(Result cr)
        {
            try
            {
                if (cr.Data is IronRuby.Builtins.Proc)
                    return new Result() { Data = cr.Data.ToString() };
            }
            catch { }

            return cr;
        }

        protected override void clearScope(ScopeParameters scopeParameters) { }

        public override ResultAssembly AddAssembly(AssemblyWrapper assembly)
        {
            Result d;

            if (assembly.FullName != null)
            {
                d = Execute("load_assembly '" + assembly.FullName + "'");
                if (d.Data is Exception)
                    d = Execute("require '" + assembly.Location + "'");
            }
            else
                d = Execute("require '" + assembly.Location + "'");

            return new ResultAssembly() { Exception = null, Loaded = true, Language = GetType() };
        }

        

        RubyIntellisense _intellisense = null;
        public override IIntellisense Intellisense
        {
            get
            {
                if (_intellisense == null) _intellisense = new RubyIntellisense(this);
                return _intellisense;
            }
        }
    }

}
